package com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.normal;

import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.TabSwitchView;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.impl.DeliverSelectTimeData;
import com.tianhui.consignortwofei.interf.impl.LoadingSelectTimeData;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleLimitInfo;
import com.tianhui.consignortwofei.widget.SelectTimeDialog;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/9/1
 * @description
 **/
public class DeliverGoodsStepThreeActivity extends DeliverGoodsActivity {

    @BindView(R.id.activity_deliver_goods_step_three_activeTimeClickItemView)
    protected ClickItemView mActiveTimeClickItemView;
    @BindView(R.id.activity_deliver_goods_step_three_deadlineTimeClickItemView)
    protected ClickItemView mDeadlineTimeClickItemView;
    @BindView(R.id.activity_deliver_goods_step_three_earliestTimeClickItemView)
    protected ClickItemView mEarliestTimeClickItemView;
    @BindView(R.id.activity_deliver_goods_step_three_deliverTimeInputItemView)
    protected InputItemView mDeliverTimeInputItemView;
    @BindView(R.id.activity_deliver_goods_step_three_distanceInputItem)
    protected InputItemView mDistanceInputItemView;
    @BindView(R.id.activity_deliver_goods_step_three_vehicleLimitClickItemView)
    protected ClickItemView mVehicleLimitClickItemView;
    @BindView(R.id.activity_deliver_goods_step_three_quoteTabSwitchView)
    protected TabSwitchView mQuoteTabSwitchView;


    private TimePickerView mActiveTimePickerView;
    //    private TimePickerView mDeadlineTimePickerView;
//    private TimePickerView mEarliestTimePickerView;


    private SelectDialog mVehicleLimitSelectDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_deliver_goods_step_three;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void initData() {
        mGoodsInfo.quotation = "0";
//        String tomorrowDate = getTomorrowDate();
//        mGoodsInfo.ordertruetime = tomorrowDate;
//        mGoodsInfo.lendtime = tomorrowDate;
//        mGoodsInfo.unloadearltime = tomorrowDate;
    }

    private String getTomorrowDate() {
        return DateUtil.getTomorrowDate("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public void initView() {
        mVehicleLimitClickItemView.setContent("车长不限");
        showStepThreeStatus();
//        mActiveTimeClickItemView.setContent(getTomorrowDate());
        mDeadlineTimeClickItemView.setContent(getTomorrowDate());
        mEarliestTimeClickItemView.setContent(getTomorrowDate());
        mDeliverTimeInputItemView.setContent("1");
    }

    @Override
    public void initListener() {

        //是否允许报价
        mQuoteTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                mGoodsInfo.quotation = value;
            }
        });

    }

    private void requestVehicleLimit() {
        mOrderModel.vehicleLimitListModel(this, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<VehicleLimitInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<VehicleLimitInfo>> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        List<VehicleLimitInfo> vehicleLimitInfoList = commonResponse.getData();
                        if (vehicleLimitInfoList != null && vehicleLimitInfoList.size() > 0) {
                            VehicleLimitInfo vehicleLimitInfo = new VehicleLimitInfo();
                            vehicleLimitInfoList.add(0, vehicleLimitInfo);
                            showVehicleLimitInfoDialog(vehicleLimitInfoList);
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void showVehicleLimitInfoDialog(List<VehicleLimitInfo> vehicleLimitInfoList) {
        mVehicleLimitSelectDialog = new SelectDialog<VehicleLimitInfo>(this);
        mVehicleLimitSelectDialog.setTitle("请选择车长吨位");
        mVehicleLimitSelectDialog.setList(vehicleLimitInfoList);
        mVehicleLimitSelectDialog.show();

        mVehicleLimitSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<VehicleLimitInfo>() {
            @Override
            public void onItemClick(VehicleLimitInfo vehicleLimitInfo, int position) {
                if (vehicleLimitInfo == null) return;

                mGoodsInfo.vehiclelengthtype = vehicleLimitInfo.id;

                mVehicleLimitClickItemView.setContent(vehicleLimitInfo.getSelectItemText());

            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

//    @OnClick(R.id.activity_deliver_goods_step_three_activeTimeClickItemView)
//    public void selectActiveTime() {
//        hideKeyboard();
//        if (mActiveTimePickerView == null) {
//
//            Calendar calendar = Calendar.getInstance();
//            int year = calendar.get(Calendar.YEAR);
//            int month = calendar.get(Calendar.MONTH);
//            int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
//            calendar.set(year, month, day);
//
//            mActiveTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
//                @Override
//                public void onTimeSelected(Date date, View v) {
//                    String time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd HH:mm:ss");
//                    mActiveTimeClickItemView.setContent(time);
//                    mGoodsInfo.ordertruetime = time;
//                }
//            })
//                    .setType(TimePickerType.ALL)
//                    .setRangDate(calendar, null)
//                    .setTitleText("请选择单据有效时间")
//                    .build();
//        }
//        mActiveTimePickerView.show();
//
//    }

    @OnClick(R.id.activity_deliver_goods_step_three_deadlineTimeClickItemView)
    public void selectDeadlineTime() {
        hideKeyboard();
//        if (mDeadlineTimePickerView == null) {
//
//            Calendar calendar = Calendar.getInstance();
//            int year = calendar.get(Calendar.YEAR);
//            int month = calendar.get(Calendar.MONTH);
//            int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
//            calendar.set(year, month, day);
//
//            mDeadlineTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
//                @Override
//                public void onTimeSelected(Date date, View v) {
//                    String time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd HH:mm:ss");
//                    mDeadlineTimeClickItemView.setContent(time);
//                    mGoodsInfo.lendtime = time;
//                }
//            })
//                    .setType(TimePickerType.ALL)
//                    .setRangDate(calendar, null)
//                    .setTitleText("请选择装货截止时间")
//                    .build();
//        }
//        mDeadlineTimePickerView.show();
        SelectTimeDialog mDeadlineSelectTimeDialog = new SelectTimeDialog(this, new LoadingSelectTimeData());
        mDeadlineSelectTimeDialog.setOnSelectTimeConfirmClickListener(new SelectTimeDialog.OnSelectTimeConfirmClickListener() {
            @Override
            public void timeConfirm(String time) {
                mDeadlineTimeClickItemView.setContent(time);
            }
        });
        mDeadlineSelectTimeDialog.show();
    }

    @OnClick(R.id.activity_deliver_goods_step_three_earliestTimeClickItemView)
    public void selectEarliestTime() {
        hideKeyboard();
//        if (mEarliestTimePickerView == null) {
//
//            Calendar calendar = Calendar.getInstance();
//            int year = calendar.get(Calendar.YEAR);
//            int month = calendar.get(Calendar.MONTH);
//            int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
//            calendar.set(year, month, day);
//
//            mEarliestTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
//                @Override
//                public void onTimeSelected(Date date, View v) {
//                    String time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd HH:mm:ss");
//                    mEarliestTimeClickItemView.setContent(time);
//                    mGoodsInfo.unloadearltime = time;
//                }
//            })
//                    .setType(TimePickerType.ALL)
//                    .setRangDate(calendar, null)
//                    .setTitleText("请选择卸货最早时间")
//                    .build();
//        }
//        mEarliestTimePickerView.show();

        SelectTimeDialog mEarliestSelectTimeDialog = new SelectTimeDialog(this, new DeliverSelectTimeData());
        mEarliestSelectTimeDialog.setOnSelectTimeConfirmClickListener(new SelectTimeDialog.OnSelectTimeConfirmClickListener() {
            @Override
            public void timeConfirm(String time) {
                mEarliestTimeClickItemView.setContent(time);
            }
        });
        mEarliestSelectTimeDialog.show();
    }

    @OnClick(R.id.activity_deliver_goods_step_three_vehicleLimitClickItemView)
    public void vehicleLimit() {
        if (mVehicleLimitSelectDialog == null) {
            requestVehicleLimit();
        } else {
            mVehicleLimitSelectDialog.show();
        }

    }

    @OnClick(R.id.activity_deliver_goods_step_three_nextButton)
    public void next() {

//        mGoodsInfo.ordertruetime = mActiveTimeClickItemView.getContent();
        String lendtime = mDeadlineTimeClickItemView.getContent();


        if (TextUtils.isEmpty(lendtime)) {
            showToast("请选择装货截止时间");
            return;
        }

        mGoodsInfo.lendtime = lendtime;
        mGoodsInfo.ordertruetime = lendtime;


//        String earliestTime = mEarliestTimeClickItemView.getContent();
//        if (TextUtils.isEmpty(earliestTime)) {
//            showToast("请选择卸车最早时间");
//            return;
//        }
//        mGoodsInfo.unloadearltime = earliestTime;
//
//        if (DateUtil.compareDate(mGoodsInfo.lendtime, mGoodsInfo.unloadearltime) == -1) {
//            showToast("卸车最早日期不能小于装货截止日期");
//            return;
//        }

        String deliverTime = mDeliverTimeInputItemView.getContent();
        if (TextUtils.isEmpty(deliverTime)) {
            showToast("请输入预计卸车时间");
            return;
        }
        mGoodsInfo.unloadearlhours = deliverTime;



        String distance = mDistanceInputItemView.getContent();
        mGoodsInfo.senddistance = distance;


        if (TextUtils.isEmpty(mGoodsInfo.senddistance)) {
            showToast("请填写发送距离");
            return;
        }

        openNextActivity();

    }

    protected void openNextActivity() {
        openNextStepActivity(DeliverGoodsStepFourActivity.class);
    }

}
